package com.qzct.controller;

import com.mybatisflex.core.paginate.Page;
import com.qzct.entity.vo.ResultCode;
import com.qzct.entity.vo.ResultVo;
import com.qzct.service.FileService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.qzct.entity.UserCommunicationImgs;
import com.qzct.service.UserCommunicationImgsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  控制层。
 *
 * @author 闽闽
 * @since 2025-01-11
 */
@RestController
@Api(tags = "种草图片")
@RequestMapping("/userCommunicationImgs")
public class UserCommunicationImgsController {

    @Autowired
    private UserCommunicationImgsService userCommunicationImgsService;


    @Autowired
    private FileService fileService;
    /**
     * 添加。
     *
     * @param userCommunicationImgs 
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    @ApiOperation("保存")
    public boolean save(@RequestBody @ApiParam("") UserCommunicationImgs userCommunicationImgs) {
        return userCommunicationImgsService.save(userCommunicationImgs);
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    @ApiOperation("根据主键")
    public boolean remove(@PathVariable @ApiParam("主键") Serializable id) {
        return userCommunicationImgsService.removeById(id);
    }

    /**
     * 根据主键更新。
     *
     * @param userCommunicationImgs 
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    @ApiOperation("根据主键更新")
    public boolean update(@RequestBody @ApiParam("主键") UserCommunicationImgs userCommunicationImgs) {
        return userCommunicationImgsService.updateById(userCommunicationImgs);
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    @ApiOperation("查询所有")
    public List<UserCommunicationImgs> list() {
        return userCommunicationImgsService.list();
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    @ApiOperation("根据主键获取")
    public UserCommunicationImgs getInfo(@PathVariable @ApiParam("主键") Serializable id) {
        return userCommunicationImgsService.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    @ApiOperation("分页查询")
    public Page<UserCommunicationImgs> page(@ApiParam("分页信息") Page<UserCommunicationImgs> page) {
        return userCommunicationImgsService.page(page);
    }
    @PostMapping(value = "/upload/images",consumes = "multipart/form-data")
    @ApiOperation("批量上传种草图片")
    public ResultVo imageUploader(@RequestPart(value = "图片") MultipartFile[] images) {
        Map<String, Object> map = new HashMap<>();
        ArrayList<String> list = new ArrayList<>();
        String filepath = null;
        for (int i = 0; i < images.length; i++) {
            filepath = "";
            System.out.println(images[i].getOriginalFilename());
            String fileName = images[i].getOriginalFilename();
            filepath = fileService.upLoadImg("test", fileName, images[i]);
            if (filepath == null) {
                return ResultVo.error(ResultCode.FILE_UPLOAD_ERROR);
            }
            list.add(filepath);
        }
        map.put("data", list);
        return new ResultVo(map);
    }
}
